﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Common.Enums;
using ZhimeiSoft.Framework.Common.MathUtils;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;


namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class frmServiceItemPlanAdd : FormBase
    {
        private int _type = 1;
        private ControlClassModeEnum _mode;
        private DBEntityServiceItemDetail _detail;
        private DBEntityCustomerServiceItemReservationTime _customerdetail;


        public ControlClassModeEnum Mode
        {
            get { return _mode; }
            set { _mode = value; }
        }

        /// <summary>
        /// 调用类型，1为设置标准服务项目服务周期，2为设置会员服务项目服务周期
        /// </summary>
        public int Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public DBEntityServiceItemDetail ServiceItemDetailInfo
        {
            get { return _detail; }
            set { _detail = value; }
        }

        public DBEntityCustomerServiceItemReservationTime CustomerServiceItemReservationTime
        {
            get { return _customerdetail; }
            set { _customerdetail = value; }
        }

        public int CustomerServiceItemID { get; set; }

        public int ServiceItemID { get; set; }

        public frmServiceItemPlanAdd()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool Validation()
        {
            string sSeqNum = txtSeqNum.Text.Trim();
            string sDays = txtDays.Text.Trim();
            string error = "";
            if (sSeqNum == string.Empty)
            {
                error = "请输入项目次数。";
                MsgBox.MsgInfo(error);
                txtSeqNum.Focus();
                SetErrorControl(txtSeqNum, error);
                return false;
            }
            if (!NumberUtil.IsNumber(sSeqNum))
            {
                error = "序号应为正整数。";
                MsgBox.MsgInfo(error);
                txtSeqNum.Focus();
                SetErrorControl(txtSeqNum, error);
                return false;
            }
            if (sDays == string.Empty)
            {
                error = "请输入间隔天数。";
                MsgBox.MsgInfo(error);
                txtDays.Focus();
                SetErrorControl(txtDays, error);
                return false;
            }
            if (!NumberUtil.IsNumber(sDays))
            {
                error = "间隔天数应为正整数。";
                MsgBox.MsgInfo(error);
                txtDays.Focus();
                SetErrorControl(txtDays, error);
                return false;
            }
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Validation() == false)
                return;

            if (_type == 1)
            {
                if (_mode == ControlClassModeEnum.emInsert)
                {
                    _detail = new DBEntityServiceItemDetail();
                    _detail.SERVICEITEMID = this.ServiceItemID;
                }
                _detail.SEQNUM = Convert.ToInt32(txtSeqNum.Text.Trim());
                _detail.SDAYS = Convert.ToInt32(txtDays.Text.Trim());
                _detail.SMEMO = txtMemo.Text;

                string error = string.Empty;
                bool bFlag = false;
                if (_mode == ControlClassModeEnum.emInsert)
                {
                    bFlag = BussinessProxyManager.Instance.InsertServiceItemDetail(_detail, out error);
                    if (bFlag == false)
                    {
                        MsgBox.MsgInfo("新增服务项目标准服务周期失败\r\n" + error);
                        return;
                    }
                }
                else if (_mode == ControlClassModeEnum.emEdit)
                {
                    bFlag = BussinessProxyManager.Instance.UpdateServiceItemDetail(_detail, out error);
                    if (bFlag == false)
                    {
                        MsgBox.MsgInfo("修改服务项目标准服务周期失败\r\n" + error);
                        return;
                    }
                }
            }
            else if (_type == 2)
            {
                if (_mode == ControlClassModeEnum.emInsert)
                {
                    _customerdetail = new DBEntityCustomerServiceItemReservationTime();
                    _customerdetail.CUSTOMERSERVICEITEMID = this.CustomerServiceItemID;
                }
                
                _customerdetail.CSEQNUM = Convert.ToInt32(txtSeqNum.Text.Trim());
                _customerdetail.CDAYS = Convert.ToInt32(txtDays.Text.Trim());
                _customerdetail.CMEMO = txtMemo.Text;

                string error = string.Empty;
                bool bFlag = false;
                if (_mode == ControlClassModeEnum.emInsert)
                    bFlag = BussinessProxyManager.Instance.InsertCustomerServiceItemReservationTime(_customerdetail, out error);
                else if (_mode == ControlClassModeEnum.emEdit)
                    bFlag = BussinessProxyManager.Instance.UpdateCustomerServiceItemReservationTime(_customerdetail, out error);
                if (bFlag == false)
                {
                    if (_mode == ControlClassModeEnum.emInsert)
                        MsgBox.MsgInfo("新增会员服务周期失败\r\n" + error);
                    else if (_mode == ControlClassModeEnum.emEdit)
                        MsgBox.MsgInfo("修改会员服务周期失败\r\n" + error);
                        
                    return;
                }
            }
            
            this.DialogResult = DialogResult.OK;
        }

        private void frmServiceItemPlanAdd_Shown(object sender, EventArgs e)
        {
            if (_mode == ControlClassModeEnum.emEdit)
            {
                string seqNum = "";
                string sDays = "";
                string sMemo = "";

                if (_type == 1)
                {
                    seqNum = _detail.SEQNUM.ToString();
                    sDays = _detail.SDAYS.ToString();
                    sMemo = _detail.SMEMO;
                }
                else if (_type == 2)
                {
                    seqNum = _customerdetail.CSEQNUM.ToString();
                    sDays = _customerdetail.CDAYS.ToString();
                    sMemo = _customerdetail.CMEMO;
                }
                txtSeqNum.Text = seqNum;
                txtDays.Text = sDays;
                txtMemo.Text = sMemo;
            }
        }
    }
}
